#include <QtGui/QApplication>
#include <QList>
#include <iostream>
#include "mainwindow.h"
#include "../shared/stl/stlfile.h"
#include "../shared/amf/amffile.h"
#include "../shared/amf/amfmesh.h"
#include "../shared/amf/amfobject.h"
#include "../shared/slicer/slicer.h"
#include "../shared/slicer/slicestack.h"
#include "../shared/slicer/slicerprogresscallback.h"
#include "printthreadprogresscallback.h"

int parseArgs(int argc, char *argv[], QString &inputFile, QString &outputFile, float &sliceHeight);

int main(int argc, char *argv[])
{
    QString inputFile = "/test.stl";
    QString outputFile = "/test.amf";
    float sliceHeight=.1;

    if ( parseArgs(argc, argv, inputFile, outputFile, sliceHeight) != 0) return -1;

    /*  QApplication a(argc, argv);
    MainWindow w;
    w.show();


    return a.exec();*/

    STLFile *stlFile = new STLFile;
    AMFFile *amfFile = new AMFFile;
    Slicer *slicer = new Slicer;
    PrintThreadSlicerProgressCallBack slicerCallback;

    stlFile->read(inputFile);
    stlFile->convertToAMF(amfFile);
    amfFile->writeToFile(outputFile);

    QList<AMFObject*> objects = amfFile->GetObjects();
    QList<AMFRegion*> regions(*objects.at(0)->GetMesh()->GetRegions());

    regions.at(0)->setFillMaterialID(1);
    std::cout << regions.at(0)->getFillMaterialId() << std::endl;

    slicer->setAlignment(Slicer::kAlignCenter);
    slicer->setSliceHeight(sliceHeight);
    slicer->doSlicing(regions.at(0), &slicerCallback);

    // Export to /sliced (hardcoded for now)
    SliceStack sliceStack_ = slicer->getSliceStack();
    QString location("/sliced/");
    sliceStack_.writeToDXF(location);
}


int parseArgs(int argc, char *argv_[], QString &inputFile, QString &outputFile, float &sliceHeight)
{
    QList<QString> argv;
    for( int i = 0; i<argc; i++)
    {
        argv.append(argv_[i]);
    }

    for (int i = 1; i < argc; i++)
    {
        if ( argv.at(i).compare("-i", Qt::CaseInsensitive) == 0 )
        {
            i++;
            inputFile = argv.at(i);
            // Check if the inputfile corresponds to a real file
            QFile inputFile_(inputFile);
            if ( inputFile_.exists() == false)
            {
                std::cout << "Invalid input file " << inputFile.toStdString() << "; File does not exist" << std::endl;
                return -1;
            } else if ( inputFile_.open(QFile::ReadOnly) == false ) {
                std::cout << "Input file " << inputFile.toStdString() << " not readable by user. Check permissions" << std::endl;
                return -1;
            }
            if ( inputFile_.isOpen() ) inputFile_.close();
        }
        else if ( argv.at(i).compare("-o", Qt::CaseInsensitive ) == 0)
        {
            i++;
            outputFile =argv.at(i);
            QFile outputFile_(outputFile);
            if ( outputFile_.exists() && !outputFile_.open(QFile::ReadWrite) )
            {
                std::cout << "Output file " << outputFile.toStdString() << " not writable by user. Check permissions" << std::endl;
                return -1;
            }
        } else if ( argv[i] == "-z" ) {
            i++;
            QString tempvar(argv[i]);
            sliceHeight = tempvar.toFloat();
        } else {
            std::cout << "Invalid parameter " << argv.at(i).toStdString() << std::endl;
            std::cout << "Usage: " << argv.at(0).toStdString() << " -i inputfile -o outputfile -z sliceheight" << std::endl;
            return -1;
        }
    }
    std::cout << "Converting " << inputFile.toStdString() << " to AMF file " << outputFile.toStdString() << std::endl;
    return 0;
}
